<?php
class comment_model_resources extends Model {
	public function __construct(){
		parent::__construct();
		$this->table = 'app_com_resources';
	}
	//添加一个资源
	public function add_resources($arguments){

		 trim_params($arguments);
		 $arguments['create_time'] = time();
		 return $this->db->insert($this->table, $arguments);
	}
	//检查res 是否存在
	public function check_res($mark){
		$key = md5($mark);
		$lifeTime = 86400*30;
		if(!$data = Cache::getInstance()->load($key)){
			$sql = "select uid,comment_num,app,control,act,id from ".$this->table." where res_id=&res_id";
			$params = array('res_id'=>$mark);
			$data = $this->db->fetchRow($sql, $params);
			if($data){
				Cache::getInstance()->save($data, $key, array(), $lifeTime);
			} else {
				return false;
			}
		}
		return $data;
	}
	public function check_res2($mark){
		$key = md5($mark);
		$lifeTime = 86400*30;
		if(!$data = Cache::getInstance()->load($key)){
			$sql = "select uid,comment_num,app,control,act,id from ".$this->table." where res_id=&res_id";
			$params = array('res_id'=>$mark);
			$data = $this->db->fetchRow($sql, $params);
			if(!$data) $data['comment_num'] = 0;
//			Cache::getInstance()->save( $data, $key ,array() , $lifeTime);
			else Cache::getInstance()->save($data, $key, array(), $lifeTime);
		}
		return $data;		
	}
	
	//读取资源 根据uid
	public function get_res_id($uid){
		$sql = "select * from ".$this->table." where uid=&uid";
		$params = array('uid'=>$uid);
		return $result = $this->db->fetchAll($sql, $params);
	}
	public function get_res_data($res_id){
		$sql = "select * from ".$this->table." where res_id=&res_id";
		$params = array('res_id'=>$res_id);
		return  $this->db->fetchRow($sql,$params);
	}
	//更新评论数
	public function update_res_comment($res_id,$num){
		$key = md5($res_id);
		Cache::getInstance()->remove($key);
		if(!$num){
			$sql = "update  ".$this->table." set comment_num=comment_num+1 where res_id='".$res_id."'";
			$this->db->query($sql);
		}else{
			$sql ="update  ".$this->table." set comment_num=comment_num-{$num} where res_id='".$res_id."'";
			$this->db->query($sql);
		}
		
	}
	
	
}